package
{
	import as3isolib.display.IsoSprite;
	
	import flash.display.Sprite;
	import flash.text.TextField;

	public class ProgressBar extends IsoSprite
	{
		private var startTime: Number;
		private var endTime: Number;
		public var building: Building;
		
		private var imgFill: ImageSprite;
		private var imgBar: ImageSprite;
		private var sprFill: IsoSprite;
		private var sprBar: IsoSprite;
		private var imageSprite:Sprite = new Sprite();
		public var init: Boolean = false;
		private var text:TextField;
		public var e:Engine;
		/**
		 * Class to hold the information for a building progress bar, showing the level of completion of a building in progress.
		 * 
		 * @param b: The building object of which this progress bar is representing.
		 * @param seconds: The length of time, in seconds, the building will take to complete from ground zero. 
		 */
		public function ProgressBar(engine:Engine,b:Building, seconds:int, xPos:int, yPos:int){
			var now:Date = new Date();
			startTime = now.getTime();
			endTime = startTime+(seconds*1000);
			
			imgFill = new ImageSprite();
			imgBar = new ImageSprite();
			imageSprite.addChild(imgFill);
			imageSprite.addChild(imgBar);
			imgFill.load(null,"assets/images/gui/userLevelFillSmall.png");
			imgBar.load(null,"assets/images/gui/userLevelBottomSmall.png");
			text = Formatting.newSubtitle("");
			imageSprite.addChild(text);
			
			this.x = xPos;
			this.y = yPos;
			building = b;
			e=engine;
		}
		
		/**
		 * Returns the amount of time remaining to build completion, in milliseconds.
		 * If the building should be complete, returns zero.
		 */
		public function getTimeRemaining():int{
			var now:Date = new Date();
			if(endTime - now.getTime() < 0) return 0;
			else return endTime - now.getTime();
		}
		
		public function updateGraphics():void{
			if (imgFill.orbitmap==null)return;
			var wd:Number = imgFill.orbitmap.width-getTimeRemaining()/(-startTime+endTime)*imgFill.orbitmap.width;
			if (wd<=2)wd=2;
			//CONSTRUCTION COMPLETE
			if (getTimeRemaining()==0){
				init=true;
				sprites=[];
				building.producing=true;
				e.progressLayer.addChild(building.getLevelSprite()); // display level number
				e.refreshScene();
				//start factory sound
				if (building.getType()==CONSTANTS.factoryType){ // FACTORY
					e.addExp(CONSTANTS.factoryExp);
					Render.factoryPlace.play();
					if (Render.factoryLoop.getPosition()==0)Render.factoryLoop.loop();
				}
				else if (building.getType()==CONSTANTS.generatorType){ // GENERATOR
					e.addExp(CONSTANTS.generatorExp);
					Render.GeneratorPlace.play();
					if (Render.GeneratorLoop.getPosition()==0)Render.GeneratorLoop.loop();
				}
				else if (building.getType()==CONSTANTS.garageType){ // GARAGE
					e.addExp(CONSTANTS.garageExp);
				}
				return;
			}
			imgFill.resizeCertain(wd,imgFill.orbitmap.height);
			text.text=""+(getTimeRemaining()/1000).toFixed(1)+" Secs Remaining";
			sprites = [imageSprite];
		}
	}
}